package com.ss.android.article.base.feature.detail2.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.util.AttributeSet;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;

public class ProgressTextView extends TextView {
    public enum Status {
        IDLE,
        DOWNLOADING,
        PAUSING,
        FAILURE,
        FINISH_INSTALL,
        FINISH_OPEN
    }


    float a;
    RectF b;
    RectF c;
    Canvas d;
    Bitmap e;
    Status f;
    private Paint g;
    private Paint h;
    private float i;
    private int j;
    private int k;
    private int l;

    public ProgressTextView(Context arg3) {
        super(arg3);
        this.f = Status.IDLE;
        this.i = UIUtils.dp2px(this.getContext(), 6f);
        this.j = R.drawable.b7;
        this.k = R.color.b9;
        this.l = R.drawable.b8;
        this.a(arg3, null);
    }

    public ProgressTextView(Context arg3, AttributeSet arg4) {
        super(arg3, arg4);
        this.f = Status.IDLE;
        this.i = UIUtils.dp2px(this.getContext(), 6f);
        this.j = R.drawable.b7;
        this.k = R.color.b9;
        this.l = R.drawable.b8;
        this.a(arg3, arg4);
    }

    private void a(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, com.ss.android.e.b.j);
            this.j = typedArray.getResourceId(com.ss.android.e.b.k, R.drawable.b7);
            typedArray.recycle();
        }

        this.setText(com.ss.android.article.browser.R.string.j2);
        this.setTextColor(this.getIdleTextColor());
        this.setBackgroundDrawable(this.getResources().getDrawable(this.j));
        this.g = new Paint(1);
        this.g.setColor(this.getReachedColor());
        this.g.setAlpha(255);
        this.g.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        this.h = new Paint(1);
        this.h.setColor(this.getBaseColor());
    }

    final void a() {
        if (this.getWidth() != 0 && this.getHeight() != 0) {
            this.e = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
            if (this.d == null) {
                this.d = new Canvas(this.e);
            } else {
                this.d.setBitmap(this.e);
            }

            this.d.drawRoundRect(this.c, this.i, this.i, this.h);
        }
    }

    public final void b() {
        this.h.setColor(this.getBaseColor());
        this.g.setColor(this.getReachedColor());
        this.g.setAlpha(255);
        switch (f) {
            case DOWNLOADING:
            case PAUSING: {
                this.a();
                this.setTextColor(this.getDownloadTextColor());
                break;
            }
            case FINISH_INSTALL:
            case FINISH_OPEN: {
                this.setBackgroundDrawable(this.getFinishBackGround());
                this.setTextColor(this.getDownloadTextColor());
                break;
            }
            case IDLE:
            case FAILURE: {
                this.setBackgroundDrawable(this.getIdleBackground());
                this.setTextColor(this.getIdleTextColor());
                break;
            }
        }

        this.invalidate();
    }

    protected int getBaseColor() {
        return this.getResources().getColor(com.ss.android.article.browser.R.color.b);
    }

    protected int getDownloadTextColor() {
        return this.getResources().getColor(com.ss.android.article.browser.R.color.ssxinzi7);
    }

    protected Drawable getFinishBackGround() {
        return this.getResources().getDrawable(this.l);
    }

    protected Drawable getIdleBackground() {
        return this.getResources().getDrawable(this.j);
    }

    protected int getIdleTextColor() {
        return this.getResources().getColor(this.k);
    }

    protected int getReachedColor() {
        return this.getResources().getColor(com.ss.android.article.browser.R.color.at);
    }

    public Status getStatus() {
        return this.f;
    }

    protected void onDraw(Canvas arg5) {
        if (this.f == Status.DOWNLOADING || this.f == Status.PAUSING) {
            this.b.right = (((float) this.getWidth())) * this.a;
            this.b.bottom = ((float) this.getHeight());
            if (this.c == null || this.c.bottom == 0f || this.c.right == 0f || this.e == null) {
                if (this.c == null) {
                    this.c = new RectF(0f, 0f, ((float) this.getWidth()), ((float) this.getHeight()));
                } else {
                    this.c.right = ((float) this.getWidth());
                    this.c.bottom = ((float) this.getHeight());
                }

                this.a();
            }

            if (this.d != null) {
                this.d.drawRect(this.b, this.g);
            }

            arg5.drawBitmap(this.e, 0f, 0f, null);
        }

        super.onDraw(arg5);
    }

    public void setProgress(float arg3) {
        this.a = arg3;
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            this.invalidate();
        } else {
            this.postInvalidate();
        }
    }

    public void setRadius(float arg1) {
        this.i = arg1;
    }

    public void setStatus(final Status arg2) {
        this.post(new Runnable() {

            public final void run() {
                float v5 = 1f;
                if (ProgressTextView.this.c == null || ProgressTextView.this.getWidth() == 0 || ProgressTextView.this.getHeight() == 0) {
                    ProgressTextView.this.c = new RectF(0f, 0f, ((float) ProgressTextView.this.getWidth()), ((float) ProgressTextView.this.getHeight()));
                }

                if (ProgressTextView.this.b == null || ProgressTextView.this.getHeight() == 0) {
                    ProgressTextView.this.b = new RectF(0f, 0f, 0f, ((float) ProgressTextView.this.getHeight()));
                }

                if (ProgressTextView.this.e == null || ProgressTextView.this.d == null) {
                    ProgressTextView.this.a();
                }

                if (arg2 == ProgressTextView.Status.IDLE || arg2 == ProgressTextView.Status.FAILURE) {
                    ProgressTextView.this.setBackgroundDrawable(ProgressTextView.this.getIdleBackground());
                    ProgressTextView.this.setTextColor(ProgressTextView.this.getIdleTextColor());
                } else {
                    if (arg2 != ProgressTextView.Status.FINISH_INSTALL && arg2 != ProgressTextView.Status.FINISH_OPEN) {
                        ProgressTextView.this.setBackgroundResource(0);
                        ProgressTextView.this.setTextColor(ProgressTextView.this.getDownloadTextColor());
                    } else {
                        ProgressTextView.this.setBackgroundDrawable(ProgressTextView.this.getFinishBackGround());
                        ProgressTextView.this.setTextColor(ProgressTextView.this.getDownloadTextColor());
                    }
                }

                switch (arg2) {
                    case IDLE: {
                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.j2);
                        break;
                    }
                    case DOWNLOADING: {
                        if (ProgressTextView.this.f == ProgressTextView.Status.DOWNLOADING) {
                            break;
                        }

                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.j_);
                        if (ProgressTextView.this.f == ProgressTextView.Status.PAUSING) {
                            break;
                        }

                        ProgressTextView.this.a();
                        ProgressTextView.this.a = 0f;
                        break;
                    }
                    case PAUSING: {
                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.rm);
                        break;
                    }
                    case FINISH_INSTALL: {
                        ProgressTextView.this.a = v5;
                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.install_now);
                        break;
                    }
                    case FINISH_OPEN: {
                        ProgressTextView.this.a = v5;
                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.rh);
                        break;
                    }
                    case FAILURE: {
                        ProgressTextView.this.setText(com.ss.android.article.browser.R.string.redownload);
                        break;
                    }
                }

                ProgressTextView.this.f = arg2;
            }
        });
    }
}

